<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="图片链接" prop="imgUrl">
        <el-input
          v-model="queryParams.imgUrl"
          placeholder="请输入图片链接"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="链接地址" prop="linkUrl">
        <el-input
          v-model="queryParams.linkUrl"
          placeholder="请输入链接地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="链接名称" prop="linkName">
        <el-input
          v-model="queryParams.linkName"
          placeholder="请输入链接名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="链接目标" prop="linkTarget">
        <el-input
          v-model="queryParams.linkTarget"
          placeholder="请输入链接目标"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:item:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:item:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:item:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:item:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <!-- 隐藏图片链接 -->
<!--      <el-table-column label="图片链接" align="center" prop="imgUrl"/>-->
      <el-table-column label="图片" align="center">
        <template slot-scope="scope">
          <ImagePreview :src="scope.row.imgUrl" width="100px" height="100px" ></ImagePreview>
        </template>
      </el-table-column>

      <el-table-column label="标题" align="center" prop="title" />
<!--      <el-table-column label="内容" align="center" prop="content" />-->
      <el-table-column label="链接地址" align="center" prop="linkUrl" >
        <template slot-scope="scope">
          {{scope.row.linkType == 2 ? scope.row.linkUrl : ''}}
        </template>
      </el-table-column>
      <el-table-column label="链接名称" align="center" prop="linkName" />
      <el-table-column label="链接类型" align="center" prop="linkType" >
        <template slot-scope="scope">
          {{scope.row.linkType == 0 ? '静态' : ''}}
          {{scope.row.linkType == 1 ? '功能跳转' : ''}}
          {{scope.row.linkType == 2 ? '超链接' : ''}}
        </template>
      </el-table-column>
      <el-table-column label="链接目标" align="center" prop="linkTarget" />
      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="statusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:item:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:item:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改轮播图对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图片链接" prop="imgUrl">
          <el-input v-model="form.imgUrl" placeholder="请输入图片链接" disabled/>
        </el-form-item>
        <el-form-item label="图片" prop="imgUrl">
          <ImageUpload limit="1" v-model="form.imgUrl" ></ImageUpload>
        </el-form-item>
<!--        <el-form-item label="标题" prop="title">-->
<!--          <el-input v-model="form.title"  placeholder="请输入标题" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="内容">-->
<!--          <textarea v-model="form.content" :min-height="500"/>-->
<!--        </el-form-item>-->
        <el-form-item label="类型">
          <el-select v-model="form.linkType"  placeholder="请选择" style="width:500px">
            <el-option
              v-for="item in dict.type.sys_item_type"
              :key="item.key"
              :label="item.label"
              :value="item.value"
              :disabled="item.raw.status === 1"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.linkType === '2'"  label="链接地址" prop="linkUrl">
          <el-input v-model="form.linkUrl" placeholder="请输入链接地址" />
        </el-form-item>
<!--        <el-form-item  label="链接名称" prop="linkName">-->
<!--          <el-input v-model="form.linkName" placeholder="请输入链接名称" />-->
<!--        </el-form-item>-->
        <el-form-item v-if="form.linkType === '1'" label="链接目标" prop="linkTarget">
          <el-select v-model="form.linkTarget"  placeholder="请选择链接目标" style="width:500px">
            <el-option
              v-for="item in artList"
              :key="item.id"
              :label="item.artName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-input type="hidden" v-model="form.status||0 " />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listItem, getItem, delItem, addItem, updateItem, updateItemStatus, getArtList} from "@/api/system/item";
import {changeRoleStatus} from "@/api/system/role";
import dict from "@/utils/dict";

export default {
  name: "Item",
  dicts:['sys_item_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 轮播图表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      artList : [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        imgUrl: null,
        title: null,
        content: null,
        linkUrl: null,
        linkName: null,
        linkType: null,
        linkTarget: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getArtList();
  } ,
  methods: {
    dict,
    /** 查询轮播图列表 */
    getList() {
      this.loading = true;
      listItem(this.queryParams).then(response => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getArtList() {
      getArtList({pageNum:1,pageSize:200}).then(response => {
        this.artList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        imgUrl: null,
        title: null,
        content: null,
        linkUrl: null,
        linkName: null,
        linkType: null,
        linkTarget: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加轮播图";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getItem(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改轮播图";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除轮播图编号为"' + ids + '"的数据项？').then(function() {
        return delItem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/item/export', {
        ...this.queryParams
      }, `item_${new Date().getTime()}.xlsx`)
    },
    statusChange(row) {
      let text= row.status === "0" ? "停用" : "启用";
      this.$modal.confirm('确认要"' + text + '""' + row.title + '"轮播图吗？').then(function() {
        return updateItemStatus(row);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
        console.log("______________" + row.status);
      }).catch(function() {
        row.status = row.status === "0" ? "0" : "1";
      });
    },
  },

};
</script>
